package com.zhuchenhua.drugmanagementsystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.zhuchenhua.drugmanagementsystem.MainActivity;
import com.zhuchenhua.drugmanagementsystem.R;

import java.lang.reflect.Method;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "HomeActivity";

    ImageView homeMainList1;
    ImageView homeMainList2;
    ImageView order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        homeMainList1 = (ImageView) findViewById(R.id.home_main_list);
        homeMainList2 = (ImageView) findViewById(R.id.home_main_list2);
        order = (ImageView) findViewById(R.id.order_icon);

        homeMainList1.setOnClickListener(this);
        homeMainList2.setOnClickListener(this);
        order.setOnClickListener(this);
    }

    /**
     * 该方法用于创建显示的menu
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.navigation_menu,menu);
        Log.d(TAG,"onCreateOptionMenu 启动了---------------------------------------------------------------------------------------");
        return true;
    }

    /**
     * 在选项菜单打开以后会调用这个方法，设置menu的图标显示
     *
     * @param featureId
     * @param menu
     * @return
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null){
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")){
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId,menu);
    }

    /**
     * 该方法对菜单的item进行点击监听
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_1:
                Toast.makeText(this,"您点击了menu_1",Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_2:
                Toast.makeText(this,"您点击了menu_2",Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_3:
                Toast.makeText(this,"您点击了menu_3",Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_4:
                Toast.makeText(this,"您点击了menu_4",Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_5:
                Toast.makeText(this,"您点击了menu_5",Toast.LENGTH_SHORT).show();
                break;
        }
        return onOptionsItemSelected(item);
    }

    /**
     * 设置点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.home_main_list:
                Intent intent1 = new Intent(HomeActivity.this, MainActivity.class);
                startActivity(intent1);
                break;
            case R.id.home_main_list2:
                Intent intent2 = new Intent(HomeActivity.this,RecordListActivity.class);
                startActivity(intent2);
                break;
            case R.id.order_icon:

                Intent intent3 = new Intent(HomeActivity.this,GoodsInfoActivity.class);
                startActivity(intent3);
                break;
            default:
                break;
        }
    }
}